package com.blog.dao.impl;

import com.blog.dao.UserDao;
import com.blog.pojo.User;
import com.blog.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner( DruidUtils.getDataSource());
    @Override
    public int addUser(User user) {
        String sql = "insert into users(username,password,phone,createdAt) values(?,?,?,?)";
        Object[] params={ user.getUsername(),user.getPassword(),user.getPhone(),user.getCreatedAt()};
        int update = 0;
        try {
            update = queryRunner.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return update;

    }

    @Override
    public int deleteUser(int id) {
        String sql="delete from users where id=?";
        Object[] params={id};
        int update = 0;
        try {
            update = queryRunner.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return update;

    }

    @Override
    public int updateUser(User user) {
       String sql="UPDATE users SET username=?,password=?,phone=? where id=?";
       Object[] params={user.getUsername(),user.getPassword(),user.getPhone(),user.getId()};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public User findUserById(int id) {
      String sql="select * from users where id=?";
      Object[] params={id};
        User query = null;
        try {
            query = queryRunner.query(sql, new BeanHandler<User>(User.class), params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return query;
    }

    @Override
    public List<User> findAll( ) {
        String sql="select * from users";
        try {
            List<User> userList = queryRunner.query(sql, new BeanListHandler<User>(User.class));
                return  userList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int addExperience(int uId, int experiencePoints) {
        String sql="update users set experiencePoints=experiencePoints+? where id=?";
        Object[] params={experiencePoints,uId};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int reduceExperience(int uId, int experiencePoints) {
        String sql="update users set experiencePoints=experiencePoints-? where id=?";
        Object[] params={experiencePoints,uId};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        }

    @Override
    public int findExperience(int uId) {
        String sql="select experiencePoints from users where id=?";
        Object[] params={uId};
        try {
            int query = queryRunner.query(sql, new BeanHandler<User>(User.class), params).getExperiencePoints();
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public User findByUserName(String username) {
        String sql="select * from users where username=?";
        Object[] params={username};
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), params);
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

}



